<?php

/**
 * 管理员
 *
 * 作者：*****
 *
 */

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\model\User;
use App\model\Attendance;
use App\model\Leave;
use App\model\Agency;
use App\model\Company;
use App\model\Role;
use App\model\Power;
use App\model\Role_power;
use App\model\User_role;
use App\model\Login_ope;
use App\model\Operation;
use DB;

class ManagerController extends Controller
{

    /** 
	 * 个人信息
	 */
	public function user()
    {
	   $id=1;
       //$model=new User();
	   //$info=$model->info($id)->toArray();
	   return view('manager.user');

    }

    /**
	 * 出勤信息
	 */
	public function out()
    {
       $model = new Attendance();
	   $info = $model->getdat();
	   return view('manager.out',[
		   'info' => $info,
	   ]);

    }

	/**
	 * 请假表信息details
	 */
	public function vacate()
    {
       $model = new Leave();
	   $info = $model->leaveList();
	   return view('manager.vacate',[
		   'info' => $info,
	   ]);

    }

	/**
	 * 工资明细表
	 */
	public function details()
    {

	   return view('manager.details');

    }

	/**
	 * 代办事项
	 */
	public function plan()
    {
       $id=1;
       $model=new Agency();
	   $info=$model->plan($id);
	   return view('manager.plan',[
		'info'=>$info   
	   ]);

    }

	/**
	 * 用户管理
	 */
	public function usename()
    {
       $model=new User();
	   $info=$model->useRole();
	   return view('manager.usename',[
		'info'=>$info   
	   ]);

    }

	/**
	 * 用户添加
	 */
	public function addUse(Request $request)
    {
        if($request->isMethod('post'))
	   {
          $use_card=$request->input('use_card');
		  $arr=User::where('use_card',$use_card)->first();
		  $use_name=$request->input('use_name');
          $pwd=$request->input('pwd');
		  $r_id=$request->input('r_id');
          if(empty($arr)){
		  
          User::insert([
			'use_name'=>$use_name,
			'use_card'=>$use_card,
			'use_pwd'=>md5($pwd),
		  ]);
		  $info=User::where('use_name',$use_name)->first()->toArray();
          User_role::insert([
			'use_id'=>$info['use_id'],
			'r_id'=>$r_id,
		  ]);
		  }else{
		   User::where('use_id',$arr->use_id)->update([
			'use_del'=>1,
			'use_pwd'=>md5($pwd),
			'use_name'=>$use_name,
			]);
           User_role::insert([
			'use_id'=>$arr->use_id,
			'r_id'=>$r_id,
		  ]);
		  
		  }

          return redirect('/manager/usename');
	   }else{
       $model=new User();
	   $role=Role::get()->toArray();
	   return view('manager.addUse',[
		'role'=>$role
	   ]);
	   }

    }

	/**
	 * 用户修改
	 */
	public function upUse(Request $request)
    {
	   if($request->isMethod('post'))
	   {
          $use_id=$request->input('use_id');
		  $r_id=$request->input('r_id');
          User_role::where('use_id',$use_id)->delete();
          User_role::insert([
			'use_id'=>$use_id,
			'r_id'=>$r_id,
		  ]);
          return redirect('/manager/usename');
	   }else{
	   $id=$request->input('id');
       $model=new User();
	   $info=$model->oneRole($id);
	   $use=User::where('use_id',$id)->select('use_id','use_name')->first()->toArray();
	   $role=Role::get()->toArray();
	   return view('manager.upUse',[
		'info'=>$info,
		'use'=>$use,
		'role'=>$role
	   ]);
	   }

    }

	/**
	 * 用户删除
	 */
	public function delUse(Request $request)
    {
       $id=$request->input('id');
       User_role::where('use_id',$id)->delete();
	   User::where('use_id',$id)->update([
			'use_del'=>0,
			]);
    }

	/**
	 * 角色管理
	 */
	public function role()
    {
	   $role=Role::get()->toArray();
       $model=new Role();
	   $info=$model->rolePower($role);
	   return view('manager.role',[
		'info'=>$info   
	   ]);

    }

	/**
	 *  角色添加
	 */
	public function addRole(Request $request)
	{
	   if($request->isMethod('post'))
	   {
	     $r_id=$request->input('r_id');
		 $role=$request->input('role');
		 Role::insert(['role'=>$role]);
		 $arr = Role::where('role',$role)->first()->toArray();
		 $id=$arr['r_id'];
		 foreach($r_id as $k=>$v){
		 DB::table('role_power')->insert([
			'r_id'=>$id,
			'p_id'=>$v,
		  ]);
		 }
		 return redirect('/manager/role');

	   }else{
	      $model=new Power();
		  $list=$model->orderPower();
		  return view('manager.addRole',[
		  'list'=>$list   
	      ]);
	   
	   }

	}

	/**
	 * 角色删除
	 */
	public function delRole(Request $request)
    {
       $id=$request->input('id');
	   Role::where('r_id',$id)->delete();
	   Role_power::where('r_id',$id)->delete();
	   User_role::where('r_id',$id)->delete();
    }

	/**
	 * 角色修改
	 */
	public function upRole(Request $request)
    {
       if($request->isMethod('post')){
           $id=$request->input('id');
	       Role_power::where('r_id',$id)->delete();
		   $r_id=$request->input('r_id');
           foreach($r_id as $k=>$v){
		    DB::table('role_power')->insert([
			'r_id'=>$id,
			'p_id'=>$v,
		   ]);
		  }

		   return redirect('/manager/role');
	   }else{
	      $id=$request->input('id');
		  $power=new Role_power();
          $powerList=$power->onePower($id);
          $powerId=array();
		  foreach($powerList as $k=>$v){
		    $powerId[]=$v['p_id'];
		  }
          $role=Role::where('r_id',$id)->first()->toArray();
	      $model=new Power();
		  $list=$model->orderPower();
		  return view('manager.upRole',[
		  'list'=>$list,
		  'powerId'=>$powerId,
		  'role'=>$role
		   
	      ]);
	      
	   }

    }




	/**
	 * 权限管理
	 */
	public function power()
    {
       $list=Power::get()->toArray();
	   $model=new Power();
       $info=$model->powerList($list);
	   if(!$info){
	    $info=array();
	   }
	   return view('manager.power',[
		'info'=>$info   
	   ]);

    }

	/**
	 * 权限添加
	 */
	public function addPower(Request $request)
    {
      if($request->isMethod('post')){
         Power::insert([
             'p_desc'=>$request->input('p_desc'),
			 'p_url'=>$request->input('p_url'),
			 'pid'=>$request->input('pid'),
       ]);
       return redirect('/manager/power');
	  }else{
		  $list=Power::where('pid',0)->get()->toArray();
	      return view('manager.addPower',[
		  'info'=>$list,
	      ]);
	    
	  }

    }

	/**
	 * 权限删除
	 */
	public function delPower(Request $request)
    {
	 
        $id=$request->input('id');
	    Power::where('p_id',$id)->delete();
		Role_power::where('p_id',$id)->delete();
	  
    }

	/**
	 *  权限修改
	 */
     public function upPower(Request $request)
    {
	   if($request->isMethod('post')){
           $id=$request->input('id');
		   $desc=$request->input('desc');
		   $url=$request->input('url');
		   $pid=$request->input('pid');
           Power::where('p_id',$id)->update([
			'p_desc'=>$desc,
			'p_url'=>$url,
            'pid'=>$pid,
			]);
		   return redirect('/manager/power');
	   }else{
	      $id=$request->input('id');
	      $list=Power::where('pid',0)->get()->toArray();
		  $arr=Power::where('p_id',$id)->first()->toArray();
	      return view('manager.upPower',[
		  'info'=>$list,
		  'arr'=>$arr
	      ]);
	      
	   }
	   
    }
	/**
	 * 公司资质/展示图片上传
	 */
    public function comimg(Request $request)
	{

       $name=$request->input('name');
	   $type=$request->input('type');
	   $preg='#<img.*src="(.*)".*/>#iUs';
	   preg_match_all($preg,$name,$title);
	   if(empty($title[1])){
	     return '请上传图片';
	   }
	   if($type==0){
	    $arr=Company::where('com_id',1)->update([
		 'com_apt'=>json_encode($title[1])
       ]);
	   }else{
	     $arr=Company::where('com_id',1)->update([
		 'com_show'=>json_encode($title[1])
       ]);
	   }
	   if($arr){
	      return "添加成功";
	   }else{
		  return '添加失败';
		}
	
	}


	/**
	 * 公司信息
	 */
	public function company(Request $request)
    {
         
	     $arr=Company::first();
	     return view('manager.cominfo',[
		 'arr'=>$arr	 
		 ]);
	   
	   }
	   

    

	/**
	 * 公司信息编辑
	 */
	public function comedit(Request $request)
    {
       if($request->isMethod('post')){
		    
           if(!$request->hasFile('file')){
              $com_logo=$request->input('com_logo','');
           }else{
		      $file = $request->file('file');
              $model=new Company();
		      $com_logo=$model->img($file);
		   }

            $arr=Company::where('com_id',1)->update([
			'com_name'=>$request->input('com_name',''),
			'com_logo'=>$com_logo,
			'com_contact'=>$request->input('com_contact',''),
			'com_phone'=>$request->input('com_phone',''),
			'com_fax'=>$request->input('com_fax',''),
			'com_introduce'=>$request->input('com_introduce',''),
		    'com_basicnews'=>$request->input('com_basicnews','')
       ]);
		  return redirect('/manager/company');

       }else{
	     $arr=Company::first();
	     return view('manager.company',[
		'arr'=>$arr	 
		 ]);
	   
	   }
	   

    }

	/**
	 * 学费
	 */
	public function stu_money()
    {

	   return view('manager.stu_money');

    }

	/**
	 * 登录操作
	 */
	public function login(Request $request)
    {
	   if($request->isMethod('post')){
	    $name=$request->input('name');
        $info=Login_ope::orderBy('lgo_id','desc')->where('use_name',$name)->get()->toArray();
	     return view('manager.login',[
		'info'=>$info   
	   ]);
	   }else{
       $info=Login_ope::orderBy('lgo_id','desc')->paginate(8);
	   return view('manager.login',[
		'info'=>$info   
	   ]);
	   }

    }

	/**
	 * 页面操作
	 */
	public function page(Request $request)
    {
	   if($request->isMethod('post')){
	   $name=$request->input('name');
       $info=Operation::orderBy('ope_id','desc')->where('use_id',$name)->get()->toArray();
	   return view('manager.page',[
		'info'=>$info   
	   ]);

	   }else{
       $info=Operation::orderBy('ope_id','desc')->paginate(8);
	   return view('manager.page',[
		'info'=>$info   
	   ]);
	   }

    }


    
}
